home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 57674 / 57674.xpi / chrome / curss.jar / content / cursitesearch.js next >
Text File  |  2010-01-03  |  5KB  |  157 lines

  1. const curss_prefserv = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  2. const wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  3.  
  4. var curss = {
  5.  
  6.     init: function()
  7.     {
  8.  
  9.         var menu = document.getElementById("contentAreaContextMenu");
  10.         menu.addEventListener("popupshowing", curss.onPopupShowing, false);
  11.  
  12.         var buttons = document.getElementById("curss-Buttons");
  13.  
  14.         const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  15.         var item = document.createElementNS(XUL_NS, "toolbarbutton");
  16.         item.setAttribute("label", "Search");
  17.         item.setAttribute("style", "cursor: pointer;list-style-image: url(chrome://cursitesearch/skin/search.ico)");
  18.         item.setAttribute("oncommand", "curss.curss_Search();");
  19.         item.setAttribute("tooltiptext", "Search");
  20.         item.setAttribute("id", "curss_search");
  21.         item.setAttribute("hidden", false);
  22.  
  23.  
  24.         buttons.appendChild(item);
  25.              
  26.     },
  27.     
  28.     onPopupShowing: function ()
  29.     {
  30.         var focusedWindow = document.commandDispatcher.focusedWindow;
  31.         var searchStr = focusedWindow.getSelection();
  32.         var item = document.getElementById("context-curss");
  33.         if (searchStr != '')
  34.             item.setAttribute("label", "Add \"" + searchStr + "\" to CurrentSiteSearch box");
  35.     },
  36.     
  37.  
  38.     curss_Search: function()
  39.     {
  40.         var isEmpty = false;
  41.         var searchTermsBox = document.getElementById("curss-SearchTerms");
  42.         var searchTerms = this.curss_TrimString(searchTermsBox.value);
  43.  
  44.         if(searchTerms.length == 0)
  45.             isEmpty = true;
  46.         else
  47.             searchTerms = this.curss_ConvertTermsToURI(searchTerms);
  48.  
  49.  
  50.         var mainWindow = wm.getMostRecentWindow("navigator:browser");
  51.         var tabbrowser = mainWindow.gBrowser;
  52.  
  53.         var hostparam = "";
  54.         try {
  55.             hostparam = "site:" + tabbrowser.currentURI.host + " ";
  56.         } catch (e) { // no site is opened on in a tab
  57.         };
  58.         if (hostparam == "site:www.google.com ") {
  59.             hostparam = "";
  60.         }
  61.  
  62.  
  63.         var URL = "http://www.google.com/custom?client=pub-6701758236670253&channel=7031513415&cof=FORID%3A13%3BAH%3Aleft%3BCX%3ACurrent%2520site%2520search%3BL%3Ahttp%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Fcustom_search_logo_sm.gif%3BLH%3A30%3BLP%3A1%3BLC%3A%230000ff%3BVLC%3A%23663399%3BDIV%3A%23336699%3B&adkw=AELymgXeH9s5blLP0YgClhe0RUurpYq-9kcTIow3hh4O2dXfNAPIqFHGA_jHQBZ6bpes-4izs4RvttGUHMhCIQx3UIkSFMfRK9MvCZazG-2WLuLgYCF-ywY&btnG=Search&cx=partner-pub-6701758236670253:xsqvgh-zggy" + "&q=" + hostparam + searchTerms;
  64.         this.curss_LoadURL(URL);    
  65.     },
  66.  
  67.     curss_TrimString: function (string)
  68.     {
  69.         // If the incoming string is invalid, or nothing was passed in, return empty
  70.         if (!string)
  71.             return "";
  72.  
  73.         string = string.replace(/^\s+/, ''); // Remove leading whitespace
  74.         string = string.replace(/\s+$/, ''); // Remove trailing whitespace
  75.         string = string.replace(/\s+/g, ' ');
  76.         return string; // Return the altered value
  77.     },
  78.  
  79.     curss_ConvertTermsToURI: function (terms)
  80.     {
  81.         var termArray = new Array();
  82.         termArray = terms.split(" ");
  83.         var result = "";
  84.  
  85.         for(var i=0; i<termArray.length; i++)
  86.         {
  87.             if(i > 0)
  88.                 result += "+";
  89.                 
  90.             result += encodeURIComponent(termArray[i]);
  91.         }
  92.  
  93.         return result; // Return the result
  94.     },
  95.  
  96.     curss_LoadURL: function (url)
  97.     {
  98.         if(curss_prefserv.getBoolPref("extensions.cursitesearch.newtab"))
  99.         {    
  100.             const newTab = gBrowser.addTab(url);
  101.             gBrowser.selectedTab = newTab;
  102.         }
  103.         else
  104.             window._content.document.location = url;
  105.             
  106.         window.content.focus();
  107.     },
  108.  
  109.  
  110.     curss_KeyHandler: function (event) 
  111.     {
  112.         if(event.keyCode == event.DOM_VK_RETURN)
  113.             this.curss_Search();
  114.     },
  115.  
  116.  
  117.     onPrefSubmit: function ()
  118.     {
  119.         return (true);
  120.     },
  121.  
  122.     eraseText: function ()
  123.     {
  124.         document.getElementById("curss-SearchTerms").value = "";
  125.     },
  126.  
  127.     addToTextbox: function (event) 
  128.     {
  129.         var focusedWindow = document.commandDispatcher.focusedWindow;
  130.         var searchStr = focusedWindow.getSelection();
  131.  
  132.         document.getElementById("curss-SearchTerms").value = searchStr;
  133.     },
  134.  
  135.     trim: function (s)
  136.     {
  137.         return rtrim(ltrim(s));
  138.     },
  139.  
  140.     ltrim: function (s)
  141.     {
  142.         var l=0;
  143.         while(l < s.length && s[l] == ' ')
  144.         {    l++; }
  145.         return s.substring(l, s.length);
  146.     },
  147.  
  148.     rtrim: function (s)
  149.     {
  150.         var r=s.length -1;
  151.         while(r > 0 && s[r] == ' ')
  152.         {    r-=1;    }
  153.         return s.substring(0, r+1);
  154.     },
  155.  
  156. };
  157. window.addEventListener("load", curss.init, false);